package com.s.core.root.strategy.factor.pics;

import com.s.core.root.data._AllData;
import com.s.core.root.strategy.base.Factor;
import com.s.core.root.strategy.base.Feature;
import com.s.core.root.strategy.factor.pic.F_Pic_LastBigMaxStrong;
import com.s.core.root.strategy.factor.pics.combination.F_Pics_LastMaxStrong;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;

/**
 * Pics特征：存在一个Pic，满足如下条件：
 * 某一个Pic是Pic_LMS，之后的MIN_LENGTH个Pic中，有LIMIT_COUNT个Pic_LMS
 * (Pic_LMS：Pic的最后一个Point是Strong且Max)
 */
@AllArgsConstructor
@NoArgsConstructor
@Data
public class F_Pics_LastBigMaxStrong implements Factor, Feature {

    private static final int MIN_LENGTH = 10;
    private static final int LIMIT_COUNT = 8;

    private int posN;

    @Override
    public void process(_AllData allData) {
        if (allData.containFeature(F_Pics_LastMaxStrong.class)) {
            List<Integer> picNs = allData.getFeature(F_Pics_LastMaxStrong.class).getNs();
            Integer indexBigMaxPosN = getPicN(picNs);
            if (indexBigMaxPosN != null) {
                int bigMaxPosN = picNs.get(indexBigMaxPosN);
                allData.picNMap.get(bigMaxPosN).addFeature(new F_Pic_LastBigMaxStrong());
                allData.addFeature(new F_Pics_LastBigMaxStrong(bigMaxPosN));
            }
        }
    }

    private Integer getPicN(List<Integer> picNs) {
        for (int i = 0; i <= picNs.size() - MIN_LENGTH + 1; i++) {
            if (count(picNs, picNs.get(i)) >= LIMIT_COUNT) {
                return i;
            }
        }
        return null;
    }

    private int count(List<Integer> picNs, int startV) {
        int count = 0;
        for (int v = startV; v < startV + MIN_LENGTH; v++) {
            if (picNs.contains(v)) {
                count++;
            }
        }
        return count;
    }
}